﻿using System;
using System.Collections.Generic;
using System.Web;
using AjeeleeCms.Core.Common;
using System.Text.RegularExpressions;

namespace AjeeleeCms.Web.tools
{
    /// <summary>
    /// post_photo 的摘要说明
    /// </summary>
    public class post_photo : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "photo": //单文件
                    postPhoto();
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void postPhoto()
        {
            Model.article_news model = new Model.article_news();
            BLL.article bll = new BLL.article();

            model.channel_id = 3;
            model.title = DTRequest.GetFormString("title");
            model.category_id = DTRequest.GetFormInt("cateid");
            model.author = DTRequest.GetFormString("author"); ;
            model.from = DTRequest.GetFormString("from");
            //摘要
            model.zhaiyao = DTRequest.GetFormString("zhaiyao");

            model.link_url = DTRequest.GetFormString("link_url");
            //检查是否有自定义图片
            model.img_url = DTRequest.GetFormString("img_url");
            model.content = DTRequest.GetFormString("content");
            model.seo_title = DTRequest.GetFormString("seo_title");
            model.seo_keywords = DTRequest.GetFormString("seo_keywords");
            model.seo_description = DTRequest.GetFormString("seo_description");
            model.sort_id = DTRequest.GetFormInt("sort_id"); ;
            model.click = 0;
            model.digg_good = 0;
            model.digg_bad = 0;
            model.is_msg = 0;
            model.is_top = 0;
            model.is_red = 0;
            model.is_hot = 0;
            model.is_slide = 0;
            model.is_lock = 0;
            //保存相册
            string reg = "<img(\\s*)+src=\"(?<imgUrl>.+?)\"(\\s*)+alt=\"(?<alt>.*?)\"(\\s*)+/>";
            string content = DTRequest.GetFormString("photos");
            Regex re = new Regex(reg, RegexOptions.IgnoreCase);
            Match mc = re.Match(content);
            if (mc.Success)
            {
                List<Model.article_albums> ls = new List<Model.article_albums>();
                MatchCollection mcs = re.Matches(content);
                foreach (Match me in mcs)
                {
                    ls.Add(new Model.article_albums { big_img = me.Groups["imgUrl"].Value, small_img = me.Groups["imgUrl"].Value, remark = me.Groups["alt"].Value });
                }
                model.albums = ls;
            }

            //扩展属性
            List<Model.attribute_value> attrls = new List<Model.attribute_value>();
            model.attribute_values = attrls;

            bll.Add(model);

        }
    }
}